<template>
  <div class="pageheader">
    <div
      v-for="(item, index) in list"
      :key="index"
      class="pageitem commhover c_text f-14 fw-6"
      :class="{ active: activeMenu == item.path }"
      @click="goPage(item)"
    >
      {{ item.meta ? item.meta.title : item.name }}
    </div>
  </div>
</template>
<script>
import { mapGetters } from "vuex";
export default {
  data() {
    return {
      active: 1,
      list: [],
    };
  },
  computed: {
    ...mapGetters(["sidebarRouters"]),
    activeMenu() {
      const route = this.$route;
      const { meta, path } = route;
      // if set path, the sidebar will highlight the path you set
      if (meta.activeMenu) {
        return meta.activeMenu;
      }
      // console.log('path', path)
      let pathAll = path.split("/")
      return pathAll[pathAll.length-1];
    },
  },
  watch: {
    $route: {
      handler: function (router) {
        let fullPath = router.fullPath;
        let firstpath = fullPath.split("/")[1];
        if (firstpath) {
          let childrenList = this.sidebarRouters.filter(
            (item) => item.path == "/" + firstpath
          );
          if (childrenList.length) {
            // 获取当前路由的全部二级菜单
            this.list = childrenList[0].children;
          }
        }
      },
      // 深度观察监听
      deep: true,
      immediate: true,
    },
  },
  methods:{
    goPage(item){
      console.log('item:',item)
      this.$router.push({path:item.path})
    },
  }
};
</script>
<style lang="scss" scoped>
.pageheader {
  width: 100%;
  height: 46px;
  position: sticky;
  top: 0;
  z-index: 8;
  height: 46px;
  padding: 0 20px;
  background: #fff;
  border-bottom: 1px solid #e9e9e9;
}
.pageitem {
  display: inline-block;
  height: 100%;
  line-height: 46px;
  border-bottom: 2px solid transparent;
  width: auto;

  &.active {
    color: #3070ff;
    border-bottom-color: #3070ff;
  }
  &:not(:first-child) {
    margin-left: 32px;
  }
}
</style>
